﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Extjs_ReGSharp.Sorting;

namespace Extjs_ReGSharp.InputParsing
{
	internal class SorterExtractor<T>
	{
		#region Consts

		private const string SortingFieldParameter = "sort";
		private const string SortingOrderParameter = "dir";

		#endregion

		#region Data Memebers

		private NameValueCollection m_InputParameters;

		#endregion

		#region Private Methods

		private SortingOrderController ExtractSortingOrder()
		{
			string orderDirection = m_InputParameters[SortingOrderParameter];

			SortingOrderController orderController = new SortingOrderController(orderDirection);

			return orderController;
		}

		private ISortingBehaviour<T> ExtractSortingBehaviour(List<ISortingBehaviourFactory<T>> sortingBehaviourFactoryList)
		{
			string fieldName = m_InputParameters[SortingFieldParameter];

			SortingBehaviourQueueOrginizer<T> behaviourOrginizer = new SortingBehaviourQueueOrginizer<T>(sortingBehaviourFactoryList);

			ISortingBehaviour<T> selectedBehaviour = behaviourOrginizer.GetSortingBehaviour(fieldName);

			return selectedBehaviour;
		}

		#endregion

		#region Constructor

		internal SorterExtractor(NameValueCollection inputParameters)
		{
			m_InputParameters = inputParameters;
		}

		#endregion

		#region Public Methods

		internal Sorter<T> ExtractSorter(List<ISortingBehaviourFactory<T>> sortingBehaviourFactoryList)
		{
			ISortingBehaviour<T> selectedBehaviour = ExtractSortingBehaviour(sortingBehaviourFactoryList);

			SortingOrderController orderController = ExtractSortingOrder();

			Sorter<T> result = new Sorter<T>(selectedBehaviour, orderController);

			return result;
		}

		#endregion
	}
}
